package com.itheima.springbootwebtlias.controller;


import com.itheima.springbootwebtlias.anno.LogOperation;
import com.itheima.springbootwebtlias.pojo.PageResult;
import com.itheima.springbootwebtlias.pojo.Result;
import com.itheima.springbootwebtlias.pojo.Student;
import com.itheima.springbootwebtlias.pojo.StudentQueryParam;
import com.itheima.springbootwebtlias.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping
    public Result queryStudent(StudentQueryParam studentQueryParam) {
        //1.接收请求参数
        log.info("请求参数：" + studentQueryParam);
        //2.调用业务层方法
        PageResult pageResult = studentService.queryStudent(studentQueryParam);
        //3.封装返回结果
        return Result.success(pageResult);
    }

    @PostMapping
    @LogOperation
    public Result addStudent(@RequestBody Student student) {
        //1.接收请求参数
        log.info("添加学员请求参数：" + student);
        //2.调用业务层方法
        studentService.addStudent(student);
        //3.封装返回结果
        return Result.success();
    }


    @GetMapping("/{id}")
    public Result selectByIdStudent(@PathVariable Integer id) {
        //1.接收请求参数
        log.info("添加学员请求参数：" + id);
        //2.调用业务层方法
        Student student = studentService.selectByIdStudent(id);

        //3.封装返回结果
        return Result.success(student);
    }

    @PutMapping
    @LogOperation
    public Result updataStudent(@RequestBody Student student) {
        //1.接收请求参数
        log.info("更新学员请求参数：" + student);
        //2.调用业务层方法
        studentService.updataStudent(student);
        log.info("返回结果");
        //3.封装返回结果
        return Result.success();
    }


    @PutMapping("/violation/{id}/{score}")
    @LogOperation
    public Result updataByIdScoreViolation(@PathVariable Integer id, @PathVariable Integer score){
        //1.接收请求参数
        log.info("添加学员请求参数学员id：" + id );
        log.info("添加学员请求参数扣除分数：：" + score );
        //2.调用业务层方法

        studentService.updataByIdScoreViolation(id,score);

        log.info("返回结果");
        //3.封装返回结果
        return Result.success();
    }


    @DeleteMapping("/{ids}")
    @LogOperation
    public Result deleteByIdStudent(@PathVariable List<Integer> ids) {
        //1.接收请求参数
        log.info("删除学员请求参数：" + ids);
        //2.调用业务层方法
        studentService.deleteByIdStudent(ids);
        log.info("返回结果");
        //3.封装返回结果
    return Result.success();

    }





    //1.接收请求参数
//    log.info("添加学员请求参数：" + );
    //2.调用业务层方法
//    log.info("返回结果");
    //3.封装返回结果
//    return Result.success();
}
